<?php

namespace app\shop\model\company;

use think\Model;
use app\shop\model\user\User;
use think\model\concern\SoftDelete;
/**
 * 应用模型
 */
class Staff extends Model
{
    use SoftDelete;
    protected $pk = "user_id";
    protected $autoWriteTimestamp = true;

    const STATE_LEAVE = 0;      //离职
    const STATE_JOB = 1;        //试用
    const STATE_REGULAR = 2;    //正式

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'user_id');
    }
    public function department()
    {
        return $this->belongsTo(Department::class, 'department_id', 'id');
    }

    public function station()
    {
        return $this->belongsTo(Station::class, 'station_id', 'id');
    }

    public function handleUser()
    {
        return $this->belongsTo(User::class, 'handle_staff', 'user_id');
    }
    
    public function getStatusTextAttr($value, $data)
    {
        $data['status'] = $data['status'] ?? 0;
        $status = [0 => '离职', 1 => '试用', '2' => '正式'];
        return $status[$data['status']];
    }

    public function getTypeTextAttr($value, $data)
    {
        $data['type'] = $data['type'] ?? 0;
        $types = [0 => '员工', 1 => '主管', '2' => '经理'];
        return $types[$data['type']];
    }

    //入职时间
    public function getJoinTimeAttr($value)
    {
        return $value ? date('Y-m-d', $value) : "";
    }

    public function setJoinTimeAttr($value)
    {
        return $value ? strtotime(date("Y-m-d", strtotime($value))) : null;
    }

    //转正时间
    public function getRegularTimeAttr($value)
    {
        return $value ? date('Y-m-d', $value) : "";
    }

    public function setRegularTimeAttr($value)
    {
        return $value ? strtotime(date("Y-m-d", strtotime($value))) : null;
    }

    //离职时间
    public function getLeaveTimeAttr($value)
    {
        return $value ? date('Y-m-d', $value) : "";
    }

    public function setLeaveTimeAttr($value)
    {
        return $value ? strtotime(date("Y-m-d", strtotime($value))) : null;
    }

    public function getNativePlaceAttr($value)
    {
        $value = $value ? explode(",", $value) : [];
        //每一项设置为数值类型
        array_walk($value, function (&$item) {
            $item = (int) $item;
        });
        return $value;
    }

    public function setNativePlaceAttr($value)
    {
        return $value ? (is_array($value) ? implode(",", $value) : $value) : "";
    }

    public function getIdcardPicAttr($value)
    {
        return $value ? explode(",", $value) : [];
    }

    public function setIdcardPicAttr($value)
    {
        return $value ? (is_array($value) ? implode(",", $value) : $value) : "";
    }

    public function getSkillPicAttr($value)
    {
        return $value ? explode(",", $value) : [];
    }

    public function setSkillPicAttr($value)
    {
        return $value ? (is_array($value) ? implode(",", $value) : $value) : "";
    }

    public function getHealthPicAttr($value)
    {
        return $value ? explode(",", $value) : [];
    }

    public function setHealthPicAttr($value)
    {
        return $value ? (is_array($value) ? implode(",", $value) : $value) : "";
    }
}
